import Vue from "vue";
import Vuex from "vuex";
import VuexPersistence from "vuex-persist";
import router,{ asyncRoutes,resetRouter,constantRoutes } from "@/router";
import { getLanguage } from "@/lang/index";
import Cookies from "js-cookie";
import {getGroupInfo} from '@/services/index'
Vue.use(Vuex);

const filter = (state) => {
  const { token, user_name, language, sidebar } = state;
  return { token, user_name, language, sidebar };
}
const vuexLocal = new VuexPersistence({
  storage: localStorage, // 存储方式
  reducer:filter
});

function filterAysncRoutes(permission,routes) {
  if(!routes) return [];
  return routes.filter(route => {
    if(route.meta && route.meta.authIndex !== undefined) {
      return permission==='all' ? true : permission[route.meta.authIndex] === '1';
    } else if(route.children && route.children.length) {
      route.children = filterAysncRoutes(permission,route.children);
       // 如果所有子路由都被过滤掉了，则父路由也应该被过滤掉
       return route.children.length > 0;
    }
    return true;
  })
}

export default new Vuex.Store({
  state: {
    token: "",
    menus: [], // 所有原始菜单
    routes: [], // 用户权限路由
    permission: '', // 用户权限
    language: getLanguage(),
    sidebar: {
      opened: true,
    },
    user_name: "",
  },
  mutations: {
    setToken(state, token) {
      state.token = token;
    },
    setMenus(state, menus) {
      state.menus = menus;
    },
    setPermission(state, permission) {
      state.permission = permission;
    },
    SET_LANGUAGE: (state, language) => {
      state.language = language;
      Cookies.set("language", language);
    },
    TOGGLE_SIDEBAR: (state) => {
      state.sidebar.opened = !state.sidebar.opened;
    },
    SET_USERNAME: (state, username) => {
      state.user_name = username;
    },
    SET_ROUTES:(state,routes) => {
      state.routes =  constantRoutes.concat(routes)
    }
  },
  actions: {
    asyncSetToken(context, args) {
      context.commit("setToken", args);
    },
    async getNewRoute({ commit }) {
      // 1111111111111111111
      // const permission = 'all'; // 测试
      // const allMenu = asyncRoutes;
      // commit("setPermission", permission);
      // commit("setMenus", allMenu);
      // commit("SET_ROUTES",allMenu);
      // allMenu.forEach(route=>{
      //   router.addRoute(route)
      // })
      // console.log('route',router.getRoutes())
      // return allMenu;
      // 1111111111111111111
      const res = await getGroupInfo();
      const permission = res.data.permission;
      const allMenu = asyncRoutes;
      commit("setPermission", permission);
      commit("setMenus", allMenu);
      const newRoutes = filterAysncRoutes(permission,allMenu);
      commit("SET_ROUTES",newRoutes);
      newRoutes.push({
				path: '*',
				redirect: '/404',
				hidden: true
			});
      router.addRoutes(newRoutes)
      return newRoutes;
    },
    resetInfo({ commit }) {
      commit("setPermission", '', {
        root: true,
      });
      commit('setToken','')
      resetRouter()
      localStorage.removeItem('vuex');
    },
  },
  plugins: [vuexLocal.plugin],
});
